package com.idol.logger.core.printer.window.panel

import android.app.Application
import android.content.Context
import android.os.Build
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import com.idol.logger.R
import com.idol.logger.core.printer.data.LoggerMo
import com.idol.logger.core.printer.window.IdolWindowPrinter
import com.idol.logger.core.printer.window.adapt.IdolV7Adapter
import com.idol.logger.core.printer.window.adapt.IdolXAdapter
import com.idol.logger.utils.findClassByClassName

internal class IdolLoggerPanel(
    private val application: Application,
    private val closeClick: () -> Unit
) {
    companion object {
        private const val ILLEGAL_STATE_MESSAGE =
            "You need dependence {androidx.recyclerview:recyclerview} / {com.android.support:recyclerview-v7}"
        private val isRecyclerX by lazy { findClassByClassName("androidx.recyclerview.widget.RecyclerView") }
        private val isRecyclerV7 by lazy { findClassByClassName("android.support.v7.widget.RecyclerView") }
    }

    private var isAttached = false

    //region ###-------------- adapter --------------------------------------------------------
    private val adapter by lazy {
        when {
            isRecyclerX -> IdolXAdapter(LayoutInflater.from(application))
            isRecyclerV7 -> IdolV7Adapter(LayoutInflater.from(application))
            else -> throw IllegalStateException(ILLEGAL_STATE_MESSAGE)
        }
    }

    fun size() = when (adapter) {
        is IdolXAdapter -> (adapter as IdolXAdapter).itemCount
        is IdolV7Adapter -> (adapter as IdolV7Adapter).itemCount
        else -> throw IllegalStateException(ILLEGAL_STATE_MESSAGE)
    }

    fun add(data: LoggerMo) = also {
        when (adapter) {
            is IdolXAdapter -> (adapter as IdolXAdapter).add(data)
            is IdolV7Adapter -> (adapter as IdolV7Adapter).add(data)
            else -> throw IllegalStateException(ILLEGAL_STATE_MESSAGE)
        }
    }
    //endregion

    //region ###-------------- FrameLayout --------------------------------------------------------
    private fun xml(): Int = when {
        isRecyclerX -> R.layout.idol_x_float_logger
        isRecyclerV7 -> R.layout.idol_7v_float_logger
        else -> throw IllegalStateException(ILLEGAL_STATE_MESSAGE)
    }

    // 日志输出面板
    private val layout: FrameLayout by lazy {
        (LayoutInflater.from(application).inflate(xml(), null) as FrameLayout).also {
            it.findViewById<View>(R.id.close).setOnClickListener {
                if (isAttached) onDetachedFromWindow()
                closeClick.invoke()
            }
        }
    }

    private val layoutParams by lazy {
        WindowManager.LayoutParams().also {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                it.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
            } else {
                it.type = WindowManager.LayoutParams.TYPE_PHONE
            }
            it.gravity = Gravity.BOTTOM
            it.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
            it.width = WindowManager.LayoutParams.MATCH_PARENT
            it.height = IdolWindowPrinter.windowHeight
        }
    }

    fun setVisibility(display: Boolean) {
        layout.visibility = if (display) View.VISIBLE else View.GONE
    }
    //endregion

    //region ###-------------- RecyclerView --------------------------------------------------------
    private val recycler by lazy {
        val v: View? = layout.findViewById(R.id.logger)
        when {
            isRecyclerX -> (v as? androidx.recyclerview.widget.RecyclerView)?.also {
                it.adapter = adapter as IdolXAdapter
                it.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(it.context)
            }
            isRecyclerV7 -> (v as? android.support.v7.widget.RecyclerView)?.also {
                it.adapter = adapter as IdolV7Adapter
                it.layoutManager = android.support.v7.widget.LinearLayoutManager(it.context)
            }
            else -> throw IllegalStateException(ILLEGAL_STATE_MESSAGE)
        }
    }

    fun smoothScrollToPosition(position: Int) {
        when {
            isRecyclerX -> {
                (recycler as androidx.recyclerview.widget.RecyclerView)
                    .smoothScrollToPosition(position)
            }
            isRecyclerV7 -> {
                (recycler as android.support.v7.widget.RecyclerView)
                    .smoothScrollToPosition(position)
            }
            else -> throw IllegalStateException(ILLEGAL_STATE_MESSAGE)
        }
    }
    //endregion

    //region ###-------------- windowManager --------------------------------------------------------
    private val windowManager: WindowManager by lazy {
        application.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    }

    fun onAttachedToWindow() {
        if (!isAttached) {
            isAttached = true
            windowManager.addView(layout, layoutParams)
        }
    }

    fun onDetachedFromWindow() {
        if (isAttached) {
            isAttached = false
            windowManager.removeView(layout)
        }
    }
    //endregion

}